package com.example.demo.consu;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class MyConsumer {

    public static void main(String[] args) {


        Properties properties = new Properties();

        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.1.1");

        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,1000);//自动提交的延迟

        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true); // 自动提交

        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserialization");

        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserialization");

        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"cwy");


        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);


        consumer.subscribe(Arrays.asList("6666","111"));

        ConsumerRecords<String,String> records = consumer.poll(Duration.ofMillis(1000));

        for (ConsumerRecord<String,String> record:
             records) {
            System.out.println("key="+record.key()+"-- value"+record.value());
        }

        consumer.close();



    }
}
